package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.mybatis.model.SysConfig;
import com.bsh.repository.DaoSysConfig;
import com.bsh.service.SysConfigService;
import com.bsh.utils.Pager;

/**
 * 系统配置
 * @author gsh
 *
 */
@Service
@Transactional
public class SysConfigServiceImpl implements SysConfigService {
	@Autowired
	DaoSysConfig daoCfg;

	/**
	 * 查询IOS版本信息
	 */
	@Override
	public JSONObject getIOSVersionConfig() {
		List<SysConfig> list=daoCfg.getIOSVersionConfig();		
		JSONObject json=new JSONObject();
		if(ApiUtils.objIsNotEmpty(list)){
			for(SysConfig syc:list){
				json.put(syc.getSkey(),syc.getSvalue());
			}
		}
		return json;
	}
	
	@Override
	public JSONObject getANDROIDVersionConfig() {
		List<SysConfig> list=daoCfg.getANDROIDVersionConfig();		
		JSONObject json=new JSONObject();
		if(ApiUtils.objIsNotEmpty(list)){
			for(SysConfig syc:list){
				json.put(syc.getSkey(),syc.getSvalue());
			}
		}
		return json;
	}

	@Override
	public List<Map<String, Object>> getAppPage(Pager pager) {
			StringBuilder sb =new StringBuilder();
			/*if(!StringUtils.isNullOrEmpty(userName))
				sb.append(String.format(" and u.name = '%s'",userName.trim()));
			if(!StringUtils.isNullOrEmpty(email))
				sb.append(String.format(" and u.email = '%s'",email.trim()));		*/
			List<Map<String, Object>> list = daoCfg.getAppPage(pager,sb.toString());
			return list;
	
	}
	
	@Override
	public List<Map<String, Object>> getSysPage(Pager pager) {
			StringBuilder sb =new StringBuilder();
			/*if(!StringUtils.isNullOrEmpty(userName))
				sb.append(String.format(" and u.name = '%s'",userName.trim()));
			if(!StringUtils.isNullOrEmpty(email))
				sb.append(String.format(" and u.email = '%s'",email.trim()));		*/
			List<Map<String, Object>> list = daoCfg.getSysPage(pager,sb.toString());
			return list;
	
	}
	
	@Override
	public SysConfig getConfig(Integer id){
		return daoCfg.selectByPrimaryKey(id);
	}

	@Override
	public Integer editConfig(Integer id, String value) {
		SysConfig cfg = daoCfg.selectByPrimaryKey(id);
		cfg.setSvalue(value);
		Integer res = daoCfg.updateByPrimaryKeyWithBLOBs(cfg);
		return res;
	}

	
}
